/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Base;

import java.sql.*;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author byron
 */
public class LoginDB {

    public boolean Loguear(String cedula, String contraseña, int bandera) throws SQLException
    {
        String consulta = null;
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        
        if (bandera == 1){
        consulta="SELECT * FROM LOGIN, ADMINISTRADOR WHERE USUARIO='" + cedula + "' AND CONTRASEÑA='" + contraseña + "' AND CEDULA_ADMINISTRADOR='"+ cedula +"'";
        } else if (bandera == 2){
        consulta="SELECT * FROM LOGIN, AYUDANTE WHERE USUARIO='" + cedula + "' AND CONTRASEÑA='" + contraseña + "' AND CEDULA_AYUDANTE='"+ cedula +"'";
        } else if (bandera == 3){
        consulta="SELECT * FROM LOGIN, EXALUMNO WHERE USUARIO='" + cedula + "' AND CONTRASEÑA='" + contraseña + "' AND CEDULA_EXALUMNO='"+ cedula +"'";
        }
        ResultSet resultado = stmt.executeQuery(consulta);
        if (resultado.next()) 
        {
            db.getConnection().close();
            return true;
        }
        db.getConnection().close();
        return false;
        
        
    }

    public boolean grabarUsuario(String usuario, String clave) throws SQLException {
    
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        boolean resultado = stmt.execute("INSERT INTO LOGIN VALUES ('" + usuario + "','" + clave +  "')");
        var.commit();
        db.getConnection().close();
        return resultado;
    }

    public String buscarClave(String cedula) throws SQLException {
 
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        

        String consulta="SELECT * FROM LOGIN WHERE USUARIO='" + cedula + "'";
        
        ResultSet resultado = stmt.executeQuery(consulta);
        if (resultado.next()) 
        {
            String ced=resultado.getString("CONTRASEÑA");
            db.getConnection().close();
            return ced;
        }
        db.getConnection().close();
        return null;
    }

    public boolean modificarClave(String cedula, String clave) throws SQLException {
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        boolean resultado = stmt.execute("UPDATE LOGIN SET CONTRASEÑA='"+clave+"' WHERE USUARIO='" + cedula + "'" );
        var.commit();
        db.getConnection().close();
        return resultado;
    }

    public void ingresarLog(String cedula, String actividad) throws SQLException {
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        CallableStatement cs = var.prepareCall("{call LOGS_ACCESO(?,?)}");
        cs.setString(1, cedula);
        cs.setString(2, actividad);
        cs.execute();
        db.getConnection().close();
        
    }
    
    
    
}
